package com.yunji.order.context.validate;

import com.yunji.cabinet.device.DeviceInfoCache;
import com.yunji.common.exception.ServiceException;
import com.yunji.common.utils.JsonUtil;
import com.yunji.coffee.entity.dto.DeviceCellDetailDTO;
import com.yunji.coffee.entity.dto.ICommonDeviceDetail;
import com.yunji.order.domain.OrderDetailDO;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author : peter-zhu
 * @date : 2025/3/22 10:23
 * @description : TODO
 **/
@Slf4j
public class OrderValidate {

    public static void orderPayValidate(String imei, List<OrderDetailDO> orderDetails) {
        List<ICommonDeviceDetail> detailList = DeviceInfoCache.getInfo(imei).getDetailList();
        Map<Long, Integer> numMap = detailList.stream().map(a -> {
            DeviceCellDetailDTO cellDetail = a.getCellDetail();
            int num = cellDetail.getProductNums();
            log.debug("orderPayValidate cellDetail :{} ",JsonUtil.toJsonString(a));
            num = num > 1 ? num - 1 : num;
            cellDetail.setProductNums(num);
            return cellDetail;
        }).collect(Collectors.groupingBy(DeviceCellDetailDTO::getProductId,
                Collectors.summingInt(DeviceCellDetailDTO::getProductNums)));
        log.info("orderPayValidate 计算出的结果:{} ", JsonUtil.toJsonString(numMap));
        for (OrderDetailDO orderDetail : orderDetails) {
            Integer pNum = numMap.get(orderDetail.getProductId());
            if (orderDetail.getProductNums() > pNum) {
                String errorMsg = "支付失败,订单号:" + orderDetail.getOrderCode() + ",商品:" + orderDetail.getProductName()
                        + "数量不够,购买数量" + orderDetail.getProductNums() + "实际设备数量" + pNum;
                log.error(errorMsg);
                throw new ServiceException(errorMsg);
            }
        }
    }

//    public static void main(String[] args) {
////        List<ICommonDeviceDetail> detailList = DeviceInfoCache.getInfo("860137073799461").getDetailList();
//        List<Grid> grids = Arrays.asList(
//                new Grid(1L, 1, 1), new Grid(1L, 2, 2), new Grid(1L, 3, 10),
//                new Grid(2L, 4, 4), new Grid(2L, 5, 0)
//        );
//        List<Purchase> buyerMsg = Arrays.asList(new Purchase(1L, 8), new Purchase(2L, 5));
//        List<Pair<Integer, Integer>> result = purchase(grids, buyerMsg);
//        result.forEach(a -> {
//            System.out.println("格子号:"+a.getFirst()+"出货数量:"+a.getSecond());
//        });
//    }
//
//    public static List<Pair<Integer, Integer>> purchase(List<Grid> grids, List<Purchase> purchases) {
//        Map<Long, List<Pair<Integer, Integer>>> map = new HashMap<>();
//        for (Purchase pNum : purchases) {
//            map.put(pNum.productId, new ArrayList<>());
//        }
//        for (Grid deviceDetail : grids) {
//            Long productId = deviceDetail.productId;
//            List<Pair<Integer, Integer>> list = map.get(productId);
//            if (deviceDetail.productNums > 0)
//                list.add(new Pair<>(deviceDetail.ordinal, deviceDetail.productNums > 1 ? deviceDetail.productNums - 1 : 1));
//        }
//        List<Pair<Integer, Integer>> orderNums = new ArrayList<>();
//        for (Purchase pNum : purchases) {
//            Long productId = pNum.productId;
//            Integer nums = pNum.productNums;
//            List<Pair<Integer, Integer>> pairList = map.get(productId);
//            while (nums > 0) {
//                for (Pair<Integer, Integer> a : pairList) {
//                    Integer ordinal = a.getFirst();
//                    Integer productNums = a.getSecond();
//                    if (productNums > nums) {
//                        orderNums.add(new Pair<>(ordinal, nums));
//                        nums = 0;
//                    }else {
//                        orderNums.add(new Pair<>(ordinal, productNums));
//                        nums = nums - productNums;
//                    }
//                }
//            }
//        }
//        return orderNums;
//    }
}
